<?php
/**    
 * 分类管理
 * @author M2
 * @version 1.0
 * create Date 2013-07-22
 */
class BlockClassifyAction extends PublicAction {
	
	/**
	 * 分类列表显示
	 */
	public function classify() {
		$list = M()->table(C('DB_PREFIX').'cms_blockclassify as blockclassify')
				->join(C('DB_PREFIX').'cms_block as block on blockclassify.id=block.cid')
				->field('blockclassify.*,count(block.id) as block_number')
				->group('blockclassify.id')
				->order('blockclassify.display_order ASC,blockclassify.id ASC')->select();
		$this->assign('list',$list);
		$this->display();
	}
	
	public function display_order() {
		display_order('cms_blockclassify');
	}
	
	/**
	 * 分类信息添加/编辑  页面显示
	 */
	public function addClassify(){
		if( !empty($_GET['id']) ){
			$data['id'] =  $_GET['id'];
			$res = M('cms_blockclassify') -> where($data) -> find();
			$this -> assign('res',$res);
		}
		$this->display();
	}
	
	/**
	 * 分类信息添加操作
	 */
	public function doAddClassify(){
		$classify_name = $_POST['classify_name'];
		if(empty($classify_name)){
			$this -> error('类型名称不能为空');
			exit;
		}
		
		$name = M('cms_blockclassify') -> where("`classify_name` = '{$classify_name}'") -> getField('classify_name');
		if($name !== null){
			$this -> error('类型名称已存在');
		} else {
			$data['classify_name'] = $_POST['classify_name'];
			$data['remark'] = $_POST['remark'];
			if (M('cms_blockclassify') -> add($data)) {
				$this->assign('jumpUrl', URL('cms/BlockClassify/classify'));
				$this->success('添加成功');
			} else {
				$this->error('添加失败');
			}
		}
	}
	
	/**
	 * 分类信息编辑操作
	 */
	public function editClassify(){
		$classify_name = $_POST['classify_name'];
		if(empty($classify_name)){
			$this -> error('类型名称不能为空');
			exit;
		}
		
		$id = M('cms_blockclassify') -> where("`classify_name` = '{$classify_name}'") -> getField('id');
		if($id !== null && $id !== $_POST['id']){
			$this -> error('类型名称已存在');
		} else {
			$data['id'] = $_POST['id'];
			$data['classify_name'] = $_POST['classify_name'];
			$data['remark'] = $_POST['remark'];
			$result = M('cms_blockclassify') -> save($data);
			if ($result !== false) {
				$this->assign('jumpUrl', URL('cms/BlockClassify/classify'));
				$this->success('修改成功');
			} else {
				$this->error('修改失败');
			}
		}
	}
	
	/**
	 * 分类信息删除操作
	 */
	public function delClassify(){
		$ids = $_POST['ids'];
		if(empty($ids)){
			echo 0;
			exit;
		}
		
		$map['id'] = array('in',$ids);
		if(M('cms_blockclassify') -> where($map) -> delete()){
			echo 1;
		} else {
			echo 0;
		}
	}
}
?>